<?php

class Settings extends Admin_Controller
{

    function __construct()
    {
        parent::__construct();
        $this->load->library('form_validation');
        $this->load->model('setting_m');
    }

    public function settings()
    {
        $post = $this->input->post();
        if ($post) {
            $this->config->set_item('webset', $post);
            $this->config->save('webset', $post);

            unset($post['qrcode']);
            foreach ($post as $k => $v) {
                $this->setting_m->sava_setting($k, ['v' => $v]);
            }

            show_message('网站配置更新成功', site_url(X_URL . 'settings/settings'), 1);
        }
        $vars         = $this->setting_m->get_setting_by_type();
        $data['vars'] = $vars;
        $data['qrcode'] = $this->config->item('qrcode');
        $this->load->view('webset/settings', $data);
    }

    public function loglist($page = 1)
    {
        /** 检查登陆 */
        if ($this->auth->is_admin()) {
            $data['title']    = '登陆日志';
            $data['siderbar'] = 'webset/allset';
            $data['submenu']  = 'webset/allset';
            $data['total']    = $this->log_m->count_log();
            //分页
            $limit                      = 20;
            $config['uri_segment']      = 4;
            $config['use_page_numbers'] = true;
            $config['base_url']         = site_url(X_URL . 'webset/loglist');
            $config['total_rows']       = $data['total'];
            $config['per_page']         = $limit;
            $config['prev_link']        = '&larr;';
            $config['prev_tag_open']    = '<li class=\'prev\'>';
            $config['prev_tag_close']   = '</li';
            $config['cur_tag_open']     = '<li class=\'active\'><span>';
            $config['cur_tag_close']    = '</span></li>';
            $config['num_tag_open']     = '<li>';
            $config['num_tag_close']    = '</li>';
            $config['next_link']        = '&rarr;';
            $config['next_tag_open']    = '<li class=\'next\'>';
            $config['next_tag_close']   = '</li>';
            $config['first_link']       = 'First Page';
            $config['first_tag_open']   = '<li class=\'first\'>';
            $config['first_tag_close']  = '</li>';
            $config['last_link']        = 'Last Page';
            $config['last_tag_open']    = '<li class=\'last\'>';
            $config['last_tag_close']   = '</li>';
            $config['num_links']        = 5;

            $this->load->library('pagination');
            $this->pagination->initialize($config);

            $start              = ($page - 1) * $limit;
            $data['pagination'] = $this->pagination->create_links();

            $data['info_list'] = $this->log_m->log_list($start, $limit);
            $this->load->view('logs_list', $data);
        } else {
            show_message('您没有此管理权限', site_url('user/login'));
        }
    }

    public function dellog($id)
    {
        /** 检查登陆 */
        if ($this->auth->is_admin()) {
            $data['title']    = '删除日志';
            $data['siderbar'] = 'webset/allset';
            $data['submenu']  = 'webset/allset';
            //删除
            if ($this->log_m->del_log($id)) {
                show_message('删除日志成功', site_url(X_URL . 'webset/loglist'), 1);
            }
        } else {
            show_message('您没有此管理权限', site_url('user/login'));
        }
    }

    public function dellog_all()
    {
        /** 检查登陆 */
        if ($this->auth->is_admin()) {
            $data['title']    = '删除日志';
            $data['siderbar'] = 'webset/allset';
            $data['submenu']  = 'webset/allset';
            //删除
            $this->log_m->del_all();
            show_message('清空日志成功', site_url(X_URL . 'webset/loglist'), 1);
        } else {
            show_message('您没有此管理权限', site_url('user/login'));
        }
    }

    public function delcaches()
    {
        $data['title']    = 'Settings';
        $data['siderbar'] = 'webset/delcaches';
        $data['submenu']  = 'webset/delcaches';
        $this->load->driver('cache', ['adapter' => 'apc', 'backup' => 'file']);
        $this->cache->clean();
        show_message('清除站内缓存成功', site_url(X_URL . 'home'), 1);
    }

}